<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2014.07.17.
   装修公司搜索($$)*/

!defined('Q_PATH') && exit;

class Index_C_Controller extends InitController{

	public function index(){
		$sKey=urldecode(trim(Q::G('key')));

		if($sKey){
			if($GLOBALS['_option_']['show_search_result_message']){
				C::urlGo(Q::U('jiaju://search/result?key='.urlencode($sKey),array(),true),1,nl2br($GLOBALS['_option_']['show_search_result_message']));
			}else{
				C::urlGo(Q::U('jiaju://search/result?key='.urlencode($sKey),array(),true));
			}
		}

		Core_Extend::getSeo($this,array('title'=>$GLOBALS['_citysite_']['site_name'].Q::L('装修公司搜索','Controller')));

		$this->assign('sKey',$sKey);
		$this->display('search+index');
	}

	public function result(){
		$sKey=urldecode(trim(Q::G('key')));
		$sKey=htmlspecialchars($sKey);
		$sKey=str_replace('%','\%',$sKey);
		$sKey=str_replace('_','\_',$sKey);

		if($sKey){
			if($GLOBALS['_option_']['search_keywords_minlength']>0 && strlen($sKey)<$GLOBALS['_option_']['search_keywords_minlength']){
				$this->E(Q::L('搜索的关键字最少为 %d 字节','Controller',null,$GLOBALS['_option_']['search_keywords_minlength']));
			}
			
			// 当前城市商家
			$arrWhere=array();
			$arrWhere['A.jiajucompany_status']=1;
			$arrWhere['string_']=Core_Extend::getCitySql('A.').
				"AND (A.jiajucompany_fullname LIKE '%{$sKey}%' OR A.jiajucompany_shortname LIKE '%{$sKey}%')";

			$nTotalRecord=Model::F_('jiajucompany','@A')->where($arrWhere)->getCounts();
			$oPage=Page::RUN($nTotalRecord,10);
			$arrJiajucompanys=Model::F_('jiajucompany','@A')
				->setColumns('A.jiajucompany_authsys,A.jiajucompany_fullname,A.jiajucompany_logo,A.jiajucompany_isfvip,A.jiajucompany_isavip,A.jiajucompany_rank,A.jiajucompany_credit,A.jiajucompany_viewnum,A.jiajucompany_address,A.jiajucompany_foundingyear,A.jiajucompany_telephone,A.jiajucompany_id,A.jiajucompany_domain')
				->where($arrWhere)
				->order('A.jiajucompany_rank DESC,A.jiajucompany_credit DESC,A.jiajucompany_id DESC')
				->limit($oPage->S(),$oPage->N())
				->getAll();

			Core_Extend::getSeo($this,array('title'=>$sKey.' - '.$GLOBALS['_citysite_']['site_name'].Q::L('装修公司搜索结果','Controller')));
			
			$this->assign('arrJiajucompanys',$arrJiajucompanys);
			$this->assign('nTotalCompanynum',$nTotalRecord);
			$GLOBALS['_commonConfig_']['URL_MODEL']=0;
			$this->assign('sPageNavbar',$oPage->P(array('id'=>'pagination','style'=>'li','current'=>'active')));
			$GLOBALS['_commonConfig_']['URL_MODEL']=2;
			$this->assign('sKey',$sKey);
			$this->display('search+result');
		}else{
			$this->U('jiaju://search/index');
		}
	}

}
